/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import pojos.ExporterItem;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import pojos.Item;
import pojos.Exporter;
import javax.transaction.UserTransaction;

/**
 *
 * @author A7med Kotb
 */
public class ExporterItemJpaController implements Serializable {

    public ExporterItemJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ExporterItem exporterItem) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Item itemID = exporterItem.getItemID();
            if (itemID != null) {
                itemID = em.getReference(itemID.getClass(), itemID.getIdItem());
                exporterItem.setItemID(itemID);
            }
            Exporter exporterID = exporterItem.getExporterID();
            if (exporterID != null) {
                exporterID = em.getReference(exporterID.getClass(), exporterID.getIdExporter());
                exporterItem.setExporterID(exporterID);
            }
            em.persist(exporterItem);
            if (itemID != null) {
                itemID.getExporterItemCollection().add(exporterItem);
                itemID = em.merge(itemID);
            }
            if (exporterID != null) {
                exporterID.getExporterItemCollection().add(exporterItem);
                exporterID = em.merge(exporterID);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findExporterItem(exporterItem.getIdExporterItem()) != null) {
                throw new PreexistingEntityException("ExporterItem " + exporterItem + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ExporterItem exporterItem) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            ExporterItem persistentExporterItem = em.find(ExporterItem.class, exporterItem.getIdExporterItem());
            Item itemIDOld = persistentExporterItem.getItemID();
            Item itemIDNew = exporterItem.getItemID();
            Exporter exporterIDOld = persistentExporterItem.getExporterID();
            Exporter exporterIDNew = exporterItem.getExporterID();
            if (itemIDNew != null) {
                itemIDNew = em.getReference(itemIDNew.getClass(), itemIDNew.getIdItem());
                exporterItem.setItemID(itemIDNew);
            }
            if (exporterIDNew != null) {
                exporterIDNew = em.getReference(exporterIDNew.getClass(), exporterIDNew.getIdExporter());
                exporterItem.setExporterID(exporterIDNew);
            }
            exporterItem = em.merge(exporterItem);
            if (itemIDOld != null && !itemIDOld.equals(itemIDNew)) {
                itemIDOld.getExporterItemCollection().remove(exporterItem);
                itemIDOld = em.merge(itemIDOld);
            }
            if (itemIDNew != null && !itemIDNew.equals(itemIDOld)) {
                itemIDNew.getExporterItemCollection().add(exporterItem);
                itemIDNew = em.merge(itemIDNew);
            }
            if (exporterIDOld != null && !exporterIDOld.equals(exporterIDNew)) {
                exporterIDOld.getExporterItemCollection().remove(exporterItem);
                exporterIDOld = em.merge(exporterIDOld);
            }
            if (exporterIDNew != null && !exporterIDNew.equals(exporterIDOld)) {
                exporterIDNew.getExporterItemCollection().add(exporterItem);
                exporterIDNew = em.merge(exporterIDNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = exporterItem.getIdExporterItem();
                if (findExporterItem(id) == null) {
                    throw new NonexistentEntityException("The exporterItem with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            ExporterItem exporterItem;
            try {
                exporterItem = em.getReference(ExporterItem.class, id);
                exporterItem.getIdExporterItem();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exporterItem with id " + id + " no longer exists.", enfe);
            }
            Item itemID = exporterItem.getItemID();
            if (itemID != null) {
                itemID.getExporterItemCollection().remove(exporterItem);
                itemID = em.merge(itemID);
            }
            Exporter exporterID = exporterItem.getExporterID();
            if (exporterID != null) {
                exporterID.getExporterItemCollection().remove(exporterItem);
                exporterID = em.merge(exporterID);
            }
            em.remove(exporterItem);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ExporterItem> findExporterItemEntities() {
        return findExporterItemEntities(true, -1, -1);
    }

    public List<ExporterItem> findExporterItemEntities(int maxResults, int firstResult) {
        return findExporterItemEntities(false, maxResults, firstResult);
    }

    private List<ExporterItem> findExporterItemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ExporterItem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ExporterItem findExporterItem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ExporterItem.class, id);
        } finally {
            em.close();
        }
    }

    public int getExporterItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ExporterItem> rt = cq.from(ExporterItem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
